home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
vol16n12.zip
/
IPC.ZIP
/
PCLIENT.ZIP
/
PCLIDLG.CPP
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
5KB
|
192 lines
// PCliDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PClient.h"
#include "PCliDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPClientDlg dialog
CPClientDlg::CPClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPClientDlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadStandardIcon(IDI_WINLOGO);
m_hPipe = NULL;
}
void CPClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPClientDlg)
DDX_Control(pDX, IDC_SERVER, m_wndServerName);
DDX_Control(pDX, IDC_CONNECT, m_wndButton);
DDX_Control(pDX, IDC_PROGRESS, m_wndProgress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPClientDlg, CDialog)
//{{AFX_MSG_MAP(CPClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_SERVER, OnChangeServerName)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_MESSAGE (WM_USER_UPDATE, OnUserUpdate)
ON_MESSAGE (WM_USER_CONNECTION_LOST, OnUserConnectionLost)
ON_BN_CLICKED (IDOK, OnConnect)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPClientDlg message handlers
BOOL CPClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_wndProgress.SetRange (0, 10);
m_wndProgress.SetPos (0);
return TRUE;
}
void CPClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CPClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPClientDlg::OnChangeServerName()
{
CString string;
m_wndServerName.GetWindowText (string);
m_wndButton.EnableWindow (string.GetLength ());
}
void CPClientDlg::OnConnect()
{
//
// Connect to the named pipe server.
//
CString strServerName;
m_wndServerName.GetWindowText (strServerName);
CString string = "\\\\" + strServerName + "\\pipe\\ipcdemo";
m_hPipe = ::CreateFile (string, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (m_hPipe == INVALID_HANDLE_VALUE) {
CString strErrMsg = "Unable to open " + string + ". " \
"Make sure the Pipe Server application is running on " \
"server " + strServerName + " and try again.";
MessageBox (strErrMsg, "Error", MB_OK | MB_ICONEXCLAMATION);
return;
}
//
// Disable further input to the edit control and Connect button.
//
m_wndServerName.EnableWindow (FALSE);
m_wndButton.EnableWindow (FALSE);
//
// Start a background thread to read input from the pipe.
//
PIPEINFO* pInfo = new PIPEINFO;
pInfo->hPipe = m_hPipe;
pInfo->hWnd = m_hWnd;
AfxBeginThread (ThreadFunc, (LPVOID) pInfo);
}
LONG CPClientDlg::OnUserUpdate (UINT wParam, LONG lParam)
{
//
// Update the progress control.
//
m_wndProgress.SetPos (wParam);
return 0;
}
LONG CPClientDlg::OnUserConnectionLost (UINT wParam, LONG lParam)
{
//
// Close the pipe handle and update the display when the connection
// is lost.
//
m_wndServerName.EnableWindow ();
m_wndButton.EnableWindow ();
m_wndProgress.SetPos (0);
::CloseHandle (m_hPipe);
m_hPipe = NULL;
return 0;
}
void CPClientDlg::OnClose()
{
//
// Close the pipe handle before shutting down.
//
if ((m_hPipe != NULL) && (m_hPipe != INVALID_HANDLE_VALUE)) {
::CloseHandle (m_hPipe);
m_hPipe = NULL;
}
CDialog::OnClose();
}
/////////////////////////////////////////////////////////////////////////////
// Thread function
UINT ThreadFunc (LPVOID pParam)
{
PIPEINFO* pInfo = (PIPEINFO*) pParam;
HANDLE hPipe = pInfo->hPipe;
HWND hWnd = pInfo->hWnd;
delete pInfo;
BYTE byte;
DWORD dwBytesRead = 999;
while (dwBytesRead && ::ReadFile (hPipe, &byte, 1, &dwBytesRead, NULL))
::PostMessage (hWnd, WM_USER_UPDATE, (WPARAM) byte, 0);
::PostMessage (hWnd, WM_USER_CONNECTION_LOST, 0, 0);
return 0;
}